/*
 * frmSystemparams.java
 *
 * Created on 6. März 2008, 07:55
 */

package com.googlecode.oskis.admin;
import java.sql.*;
import com.googlecode.oskis.library.db.DBQuery;
import com.googlecode.oskis.library.db.ComboBoxFiller;
import com.googlecode.oskis.library.db.ComboBoxEintrag;
import com.googlecode.oskis.library.db.SQLErrorDialog;
import com.googlecode.oskis.library.date.NOW;
import javax.swing.JOptionPane;

/**
 *
 * @author  thomas
 */
public class frmSystemparams extends javax.swing.JFrame {
    
    private Connection db;
    private DBQuery q;
    
    
    /** Creates new form frmSystemparams */
    public frmSystemparams(Connection db) {
        initComponents();
        this.db=db;
        q=new DBQuery(db,"select kategorie,parameter,bezeichnung,value from sparams");
        ComboBoxFiller cbf=new ComboBoxFiller(db);
        NOW now = new NOW();
        cbf.fill(cmbKategorie,"select kategorie,bezeichnung from sparamkat where "+now.getDBRepresentation()+" between datf and datt");        
    }
    
    
    
    private void refreshTable() {
        if (q.executeQuery()) {
            tblParams.setModel(q.getTableModel());
        } else {
            JOptionPane.showMessageDialog(this,q.getSQLError());
        }
    }
    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblKategorie = new javax.swing.JLabel();
        cmbKategorie = new javax.swing.JComboBox();
        lblParameter = new javax.swing.JLabel();
        txtParameter = new javax.swing.JTextField();
        lblValue = new javax.swing.JLabel();
        txtValue = new javax.swing.JTextField();
        btnNew = new javax.swing.JButton();
        btnEdit = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblParams = new javax.swing.JTable();
        lblBezeichnung = new javax.swing.JLabel();
        txtBezeichnung = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Systemparameter");

        lblKategorie.setText("Kategorie");

        cmbKategorie.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        lblParameter.setText("Parameter");

        lblValue.setText("Wert");

        btnNew.setMnemonic('N');
        btnNew.setText("Neu");
        btnNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNewActionPerformed(evt);
            }
        });

        btnEdit.setMnemonic('e');
        btnEdit.setText("Ändern");
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });

        btnDelete.setMnemonic('L');
        btnDelete.setText("Löschen");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });

        tblParams.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tblParams);

        lblBezeichnung.setText("Bezeichnung");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblBezeichnung)
                            .addComponent(lblValue)
                            .addComponent(lblParameter)
                            .addComponent(lblKategorie))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cmbKategorie, 0, 296, Short.MAX_VALUE))
                            .addComponent(txtParameter, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 296, Short.MAX_VALUE)
                            .addComponent(txtValue, javax.swing.GroupLayout.DEFAULT_SIZE, 296, Short.MAX_VALUE)
                            .addComponent(txtBezeichnung, javax.swing.GroupLayout.DEFAULT_SIZE, 296, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(btnNew)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnEdit)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnDelete)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblKategorie)
                    .addComponent(cmbKategorie, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblParameter)
                    .addComponent(txtParameter, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblBezeichnung)
                    .addComponent(txtBezeichnung, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblValue)
                    .addComponent(txtValue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnNew)
                    .addComponent(btnEdit)
                    .addComponent(btnDelete))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 263, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNewActionPerformed
        String kategorie=((ComboBoxEintrag)cmbKategorie.getSelectedItem()).getKey();
        String parameter=txtParameter.getText();
        String bezeichnung=txtBezeichnung.getText();
        String value=txtValue.getText();
        String SQL="insert into sparams(kategorie,parameter,bezeichnung,value)" +
                "   values('"+kategorie+"','"+parameter+"','"+bezeichnung+"','"+value+"')";
        try {
            Statement st=db.createStatement();
            st.execute(SQL);
            st.close();
            refreshTable();        
        } catch (SQLException e) {
            new SQLErrorDialog(this,"PG",e,SQL).setVisible(true);
        }
    }//GEN-LAST:event_btnNewActionPerformed

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
        String kategorie=((ComboBoxEintrag)cmbKategorie.getSelectedItem()).getKey();
        String parameter=txtParameter.getText();
        String bezeichnung=txtBezeichnung.getText();
        String value=txtValue.getText();
        String SQL="update sparams" +
                "     set bezeichnung='"+bezeichnung+"'," +
                "         value='"+value+"'" +
                "     where kategorie='"+kategorie+"'"+
                "     and parameter='"+parameter+"'";
        try {
            Statement st=db.createStatement();
            st.execute(SQL);
            st.close();
            refreshTable();        
        } catch (SQLException e) {
            new SQLErrorDialog(this,"PG",e,SQL).setVisible(true);
        }
    }//GEN-LAST:event_btnEditActionPerformed

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        String kategorie=((ComboBoxEintrag)cmbKategorie.getSelectedItem()).getKey();
        String parameter=txtParameter.getText();
        String SQL="delete from sparams" +
                "     where kategorie='"+kategorie+"'"+
                "     and parameter='"+parameter+"'";
        try {
            Statement st=db.createStatement();
            st.execute(SQL);
            st.close();
            refreshTable();        
        } catch (SQLException e) {
            new SQLErrorDialog(this,"PG",e,SQL).setVisible(true);
        }
    }//GEN-LAST:event_btnDeleteActionPerformed
    

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton btnNew;
    private javax.swing.JComboBox cmbKategorie;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblBezeichnung;
    private javax.swing.JLabel lblKategorie;
    private javax.swing.JLabel lblParameter;
    private javax.swing.JLabel lblValue;
    private javax.swing.JTable tblParams;
    private javax.swing.JTextField txtBezeichnung;
    private javax.swing.JTextField txtParameter;
    private javax.swing.JTextField txtValue;
    // End of variables declaration//GEN-END:variables
    
}
